#ifndef _SPARSE_DATA_SET
#define _SPARSE_DATA_SET

#include "SparseData.h"

#include <fstream>
#include <vector>
#include <cstdio>
#include <cstdlib>


struct SparseDataSet
    : BasicSerializable
{

public:

    virtual ~SparseDataSet()
    {
        for (INT i = 0; i < dataSet.size(); ++i)
        {
            SparseData::deleteInstance(dataSet[i], SparseData::insName(), m_MemManager);
        }
    }

    VOID initialize()
    {
        m_BufferSize = 1024 * 1024;
    }

    VOID regMemoryManager(IN MemoryManager* _mem)
    {
        m_MemManager = _mem;
    }

    VOID setBufferSize(IN INT _size)
    {
        m_BufferSize = _size;
    }

    VOID LoadData(IN STRING _fileName)
    {
        FILE* fin = fopen(_fileName.c_str(), "r");
        CHAR* buffer = m_MemManager->newT_A(buffer, m_BufferSize);
        while (fgets(buffer, m_BufferSize, fin))
        {
            SparseData* sd = SparseData::newInstance(SparseData::insName(), m_MemManager);
            sd->parseData(buffer);
            dataSet.push_back(sd);
        }
        m_MemManager->deleteT(buffer);
        fclose(fin);
    }

    VOID LoadDataNoId(IN STRING _fileName)
    {
        FILE* fin = fopen(_fileName.c_str(), "r");
        CHAR* buffer = m_MemManager->newT_A(buffer, m_BufferSize);
        while (fgets(buffer, m_BufferSize, fin))
        {
            SparseData* sd = SparseData::newInstance(SparseData::insName(), m_MemManager);
            sd->parseDataNoId(buffer);
            dataSet.push_back(sd);
        }
        m_MemManager->deleteT(buffer);
        fclose(fin);
    }

    typedef std::vector<SparseData*> DATA_SET;
    DATA_SET dataSet;

public:

    BOOL serializable()
    {
        return TRUE;
    }

    VOID serialize(OUT BYTE* _out)
    {
        UINT32 len = dataSet.size();
        UINT32 offset = 0;
        memcpy(_out, &len, sizeof(len));
        offset += sizeof(len);
        for (INT i = 0; i < len; ++i)
        {
            dataSet[i]->serialize(_out + offset);
            offset += dataSet[i]->binLength();
        }
    }

    VOID deSerialize(IN BYTE* _in)
    {
        UINT32 len;
        UINT32 offset = 0;
        memcpy(&len, _in, sizeof(len));
        offset += sizeof(len);
        for (INT i = 0; i < len; ++i)
        {
            SparseData* sd = SparseData::newInstance("SparseData", m_MemManager);
            sd->deSerialize(_in + offset);
            offset += sd->binLength();
            dataSet.push_back(sd);
        }
    }

    UINT32 binLength()
    {
        UINT32 ret = sizeof(UINT32);
        for (INT i = 0; i < dataSet.size(); ++i)
        {
            ret += dataSet[i]->binLength();
        }
        return ret;
    }

protected:

    MemoryManager* m_MemManager;
    INT m_BufferSize;

};

#endif // #ifndef _SPARSE_DATA_SET